home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / scripts / mkestrres.py < prev    next >
Encoding:
Python Source  |  1996-09-05  |  2.8 KB  |  127 lines  |  [TEXT/Pyth]

  1. """Parse sys/errno.h and Errors.h and create Estr resource"""
  2.  
  3. import regex
  4. import macfs
  5. import string
  6. import Res
  7. import os
  8.  
  9. READ = 1
  10. WRITE = 2
  11. smAllScripts = -3
  12.  
  13. ERRNO_PROG="#define[ \t]+" \
  14.            "\([A-Z0-9a-z_]+\)" \
  15.            "[ \t]+" \
  16.            "\([0-9]+\)" \
  17.            "[ \t]*/\*[ \t]*" \
  18.            "\(.*\)" \
  19.            "[ \t]*\*/"
  20.            
  21. ERRORS_PROG="[ \t]*" \
  22.             "\([A-Z0-9a-z_]+\)" \
  23.             "[ \t]*=[ \t]*" \
  24.             "\([-0-9]+\)" \
  25.             "[, \t]*/\*[ \t]*" \
  26.             "\(.*\)" \
  27.             "[ \t]*\*/"
  28.  
  29. def Pstring(str):
  30.     if len(str) > 255:
  31.         raise ValueError, 'String too large'
  32.     return chr(len(str))+str
  33.     
  34. def writeestr(dst, edict):
  35.     """Create Estr resource file given a dictionary of errors."""
  36.     
  37.     os.unlink(dst.as_pathname())
  38.     Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
  39.     output = Res.FSpOpenResFile(dst, WRITE)
  40.     Res.UseResFile(output)
  41.     for num in edict.keys():
  42.         res = Res.Resource(Pstring(edict[num][0]))
  43.         res.AddResource('Estr', num, '')
  44.         res.WriteResource()
  45.     Res.CloseResFile(output)
  46.     
  47. def writepython(fp, dict):
  48.     k = dict.keys()
  49.     k.sort()
  50.     for i in k:
  51.         fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
  52.     
  53.  
  54. def parse_errno_h(fp, dict):
  55.     errno_prog = regex.compile(ERRNO_PROG)
  56.     for line in fp.readlines():
  57.         if errno_prog.match(line) > 0:
  58.             number = string.atoi(errno_prog.group(2))
  59.             name = errno_prog.group(1)
  60.             desc = string.strip(errno_prog.group(3))
  61.             
  62.             if not dict.has_key(number):
  63.                 dict[number] = desc, name
  64.             else:
  65.                 print 'DUPLICATE', number
  66.                 print '\t', dict[number]
  67.                 print '\t', (desc, name)
  68.                                 
  69. def parse_errors_h(fp, dict):
  70.     errno_prog = regex.compile(ERRORS_PROG)
  71.     for line in fp.readlines():
  72.         if errno_prog.match(line) > 0:
  73.             number = string.atoi(errno_prog.group(2))
  74.             name = errno_prog.group(1)
  75.             desc = string.strip(errno_prog.group(3))
  76.             if number > 0: continue
  77.             
  78.             if not dict.has_key(number):
  79.                 dict[number] = desc, name
  80.             else:
  81.                 print 'DUPLICATE', number
  82.                 print '\t', dict[number]
  83.                 print '\t', (desc, name)
  84.             
  85. def main():
  86.     dict = {}
  87.     fss, ok = macfs.PromptGetFile("Where is errno.h?")
  88.     if not ok: return
  89.     fp = open(fss.as_pathname())
  90.     parse_errno_h(fp, dict)
  91.     fp.close()
  92.     
  93.     fss, ok = macfs.PromptGetFile("Where is Errors.h?")
  94.     if not ok: return
  95.     fp = open(fss.as_pathname())
  96.     parse_errors_h(fp, dict)
  97.     fp.close()
  98.     
  99.     if not dict:
  100.         return
  101.         
  102.     fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc")
  103.     if ok:
  104.         writeestr(fss, dict)
  105.     
  106.     fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py")
  107.     if ok:
  108.         fp = open(fss.as_pathname(), "w")
  109.         writepython(fp, dict)
  110.         fp.close()
  111.         fss.SetCreatorType('Pyth', 'TEXT')
  112.  
  113.     fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt")
  114.     if ok:
  115.         fp = open(fss.as_pathname(), "w")
  116.         
  117.         k = dict.keys()
  118.         k.sort()
  119.         for i in k:
  120.             fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
  121.         fp.close()
  122.  
  123.     
  124. if __name__ == '__main__':
  125.     main()
  126.     
  127.